﻿package export.pet
{
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.data.vo.PetDataVO;
	import com.game.data.vo.PlayerDataVO;
	import com.game.event.GameEvent;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import com.game.manager.NotificationManager;
	import com.game.view.component.notification.TipBox;
	
	import config.Config;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class PetInterface extends Sprite
	{
		
		public function PetInterface()
		{
			super();
			TextUtil.redrawTxtOld(this, this.needRedrawText);
			this.gc = Config.getIns();
			this.addEventListener(Event.ADDED_TO_STAGE, this.added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.removed, false, 0, true);
		}
		
		public var btn_close:SimpleButton;
		public var releasebtn:SimpleButton;
		public var fightbtn:SimpleButton;
		public var restbtn:SimpleButton;
		public var upBtn:SimpleButton;
		public var hptxt:TextField;
		public var mptxt:TextField;
		public var atktxt:TextField;
		public var deftxt:TextField;
		public var expmc:MovieClip;
		public var lifetimemc:MovieClip;
		public var defqualitymc:MovieClip;
		public var atkqualitymc:MovieClip;
		public var mpqualitymc:MovieClip;
		public var hpqualitymc:MovieClip;
		public var exptxt:TextField;
		public var lifetimetxt:TextField;
		public var defqualitytxt:TextField;
		public var atkqualitytxt:TextField;
		public var mpqualitytxt:TextField;
		public var hpqualitytxt:TextField;
		public var speedtxt:TextField;
		public var eamptxt:TextField;
		public var mdeftxt:TextField;
		public var crittxt:TextField;
		public var misstxt:TextField;
		public var eahptxt:TextField;
		public var skill1:Sprite;
		public var skill2:Sprite;
		public var skill3:Sprite;
		public var skill4:Sprite;
		public var skill5:Sprite;
		public var skill6:Sprite;
		public var skill7:Sprite;
		public var skill8:Sprite;
		public var warpowertxt:TextField;
		public var techniquetxt:TextField;
		public var perceptiontxt:TextField;
		public var qualitymc:MovieClip;
		public var txtname:TextField;
		public var listtxt:TextField;
		public var leveltxt:TextField;
		public var prePage:SimpleButton;
		public var nextPage:SimpleButton;
		public var czsxbtn:SimpleButton;
		public var czjnbtn:SimpleButton;
		private var gc:Config;
		private var player:PlayerDataVO;
		private var pif:PetDataVO;
		private var pethead:PetHeadSprite;
		private var curPage:uint = 1;
		private var allPage:uint = 2;
		private var pageNum:uint = 5;
		private var btnState:*;
		private var lastBtn:String = "";
		private var needRedrawText:Array = ["hptxt", "mptxt", "atktxt", "deftxt", "exptxt", "lifetimetxt", "defqualitytxt", "atkqualitytxt", "mpqualitytxt", "hpqualitytxt", "speedtxt", "eamptxt", "mdeftxt", "crittxt", "misstxt", "eahptxt", "warpowertxt", "techniquetxt", "perceptiontxt", "txtname", "listtxt", "leveltxt"];
		
		public function setRole(param1:PlayerDataVO):void
		{
			this.player = param1;
		}
		
		public function setCurPage(param1:int):void
		{
			var _loc2_:uint = this.curPage;
			this.curPage += param1;
			if (this.curPage > this.allPage)
			{
				this.curPage = this.allPage;
			}
			if (this.curPage < 1)
			{
				this.curPage = 1;
			}
			if (this.curPage != _loc2_)
			{
				this.setPetList();
			}
		}
		
		public function setPetInfomation(param1:PetDataVO):void
		{
			this.pif = param1;
			this.setShow();
			this.setPetAllSkill();
		}
		
		private function setPetList(param1:Boolean = false):void
		{
			var _loc2_:* = null;
			var _loc3_:* = null;
			this.removedPetList();
			this.setTitleTxt();
			var _loc4_:int = 0;
			while (_loc4_ < 5)
			{
				_loc3_ = ResUtil.getNewObj("fla::petlist") as Sprite;
				TextUtil.redrawTxtOld(_loc3_, ["petname"]);
				_loc3_.buttonMode = true;
				if (this.player.petList[_loc4_ + (this.curPage - 1) * 5] != undefined)
				{
					if (this.player.petList[_loc4_ + (this.curPage - 1) * 5].isFight == 1)
					{
						_loc3_["petname"].text = this.player.petList[_loc4_ + (this.curPage - 1) * 5].getPetChinaName() + "(出战)";
						_loc2_ = _loc3_;
					}
					else
					{
						_loc3_["petname"].text = this.player.petList[_loc4_ + (this.curPage - 1) * 5].getPetChinaName();
					}
					_loc3_.name = "petlist" + _loc4_;
					_loc3_.x = 349.85;
					_loc3_.y = 140 + _loc4_ * Number(_loc3_.height) + 5;
					this.addChild(_loc3_);
					_loc3_.addEventListener(MouseEvent.CLICK, this.plClick);
				}
				_loc4_++;
			}
			if (param1)
			{
				if (_loc2_)
				{
					_loc2_.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
				}
			}
		}
		
		private function removedPetList():void
		{
			var _loc1_:int = 0;
			while (_loc1_ < 5)
			{
				if (this.getChildByName("petlist" + _loc1_))
				{
					this.getChildByName("petlist" + _loc1_).removeEventListener(MouseEvent.CLICK, this.plClick);
					this.removeChild(this.getChildByName("petlist" + _loc1_));
				}
				_loc1_++;
			}
		}
		
		private function findPetInCurrentPetListById(param1:uint):PetDataVO
		{
			if (this.player.petList[param1])
			{
				return this.player.petList[param1];
			}
			return null;
		}
		
		private function addPetHead():void
		{
			if (this.pethead)
			{
				if (contains(this.pethead))
				{
					this.removeChild(this.pethead);
				}
				this.pethead.destroy();
				this.pethead = null;
			}
			this.pethead = new PetHeadSprite(this.pif.petName);
			this.addChild(this.pethead);
			this.pethead.x = 280;
			this.pethead.y = 220;
			this.setChildIndex(this.pethead, this.getChildIndex(this.releasebtn) - 1);
		}
		
		private function setPetAllSkill():void
		{
			var _loc1_:int = 0;
			var _loc2_:* = null;
			var _loc3_:* = 0;
			_loc1_ = 0;
			while (_loc1_ < 8)
			{
				this["skill" + (_loc1_ + 1)].removeSkill();
				_loc1_++;
			}
			if (this.pif)
			{
				_loc2_ = {};
				_loc3_ = uint(this.pif.skill.length);
				_loc1_ = 0;
				while (_loc1_ < _loc3_)
				{
					_loc2_.sname = this.pif.skill[_loc1_].sname;
					_loc2_.cname = this.pif.transPetChinaSkillName(this.pif.skill[_loc1_].sname);
					_loc2_.sinfo = this.pif.skill[_loc1_].sinfo;
					this["skill" + (_loc1_ + 1)].setCurrentSkill(_loc2_);
					_loc1_++;
				}
			}
		}
		
		private function setShow():void
		{
			if (this.pif)
			{
				this.qualitymc.gotoAndStop(this.pif.quality);
				this.setTitleTxt();
				this.leveltxt.text = this.pif.level.toString();
				this.txtname.text = this.pif.petChinaName;
				this.hptxt.text = this.pif.hp + "/" + this.pif.hpMax;
				this.mptxt.text = this.pif.mp + "/" + this.pif.mpMax;
				this.atktxt.text = this.pif.atk.toString();
				this.deftxt.text = this.pif.def.toString();
				this.perceptiontxt.text = this.pif.perception.toString();
				this.techniquetxt.text = this.pif.technique.toString();
				this.warpowertxt.text = this.pif.warpower.toString();
				this.exptxt.text = this.pif.curExp + "/" + this.pif.getPetNextLevelExp();
				this.lifetimetxt.text = this.pif.lifetime + "/" + 100;
				this.defqualitytxt.text = this.pif.defQuality + "/" + 2000;
				this.atkqualitytxt.text = this.pif.atkQuality + "/" + 2000;
				this.mpqualitytxt.text = this.pif.mpQuality + "/" + 2000;
				this.hpqualitytxt.text = this.pif.hpQuality + "/" + 2000;
				this.expmc.gotoAndStop(Math.round(20 * this.pif.curExp / this.pif.getPetNextLevelExp()) + 1);
				this.speedtxt.text = this.pif.moveSpeed.toString();
				this.eamptxt.text = this.pif.mpHeal.toString();
				this.eahptxt.text = this.pif.hpHeal.toString();
				this.crittxt.text = int(this.pif.crit) + "%";
				this.misstxt.text = Math.round(this.pif.miss) + "%";
				this.mdeftxt.text = Math.round(this.pif.magicDef) + "%";
				this.lifetimemc.gotoAndStop(20 - Math.round(20 * this.pif.lifetime / 100));
				this.defqualitymc.gotoAndStop(Math.round(20 * this.pif.defQuality / 2000) + 1);
				this.atkqualitymc.gotoAndStop(Math.round(20 * this.pif.atkQuality / 2000) + 1);
				this.mpqualitymc.gotoAndStop(Math.round(20 * this.pif.mpQuality / 2000) + 1);
				this.hpqualitymc.gotoAndStop(Math.round(20 * this.pif.hpQuality / 2000) + 1);
			}
			else
			{
				this.qualitymc.gotoAndStop(3);
				this.txtname.text = "";
				this.hptxt.text = "";
				this.mptxt.text = "";
				this.atktxt.text = "";
				this.deftxt.text = "";
				this.perceptiontxt.text = "";
				this.techniquetxt.text = "";
				this.warpowertxt.text = "";
				this.exptxt.text = "";
				this.lifetimetxt.text = "";
				this.defqualitytxt.text = "";
				this.atkqualitytxt.text = "";
				this.mpqualitytxt.text = "";
				this.hpqualitytxt.text = "";
				this.leveltxt.text = "";
				this.expmc.gotoAndStop(1);
				this.speedtxt.text = "";
				this.eamptxt.text = "";
				this.eahptxt.text = "";
				this.crittxt.text = "";
				this.misstxt.text = "";
				this.mdeftxt.text = "";
				this.lifetimemc.gotoAndStop(20);
				this.defqualitymc.gotoAndStop(1);
				this.atkqualitymc.gotoAndStop(1);
				this.mpqualitymc.gotoAndStop(1);
				this.hpqualitymc.gotoAndStop(1);
			}
		}
		
		private function setTitleTxt():void
		{
			this.listtxt.text = "宠物列表" + this.player.petList.length + "/10";
		}
		
		private function AfterSuperRevolution():void
		{
			if (!this.pif)
			{
				NotificationManager.alert("请先选择宠物");
				return;
			}
			this.addPetHead();
			this.refreshPetList();
			if (this.pif.perception < 8)
			{
				this.pif.perception++;
			}
			this.setPetAllSkill();
			this.setShow();
		}
		
		private function sendHeroRefreshPet():void
		{
			if (this.gc.team.hero)
			{
				if (this.gc.team.hero.getPlayer() == this.player)
				{
					this.gc.team.hero.changePet();
				}
			}
			EventManager.getIns().dispatchEvent(GameEventType.REFRESH_PET);
		}
		
		private function refreshPetList():void
		{
			var _loc1_:Boolean = false;
			var _loc2_:uint = this.player.petList.length;
			while (_loc2_-- > 0)
			{
				if (this.getChildByName("petlist" + _loc2_))
				{
					if (this.getChildByName("petlist" + _loc2_))
					{
						if (this.player.petList[_loc2_].isFight == 1)
						{
							if (!_loc1_)
							{
								this.getChildByName("petlist" + _loc2_)["petname"].text = this.player.petList[_loc2_].getPetChinaName() + "(出战)";
								_loc1_ = true;
							}
							else
							{
								this.player.petList[_loc2_].isFight = 0;
							}
						}
						else
						{
							this.getChildByName("petlist" + _loc2_)["petname"].text = this.player.petList[_loc2_].getPetChinaName();
						}
					}
				}
			}
		}
		
		private function added(param1:Event):void
		{
			this.btn_close.addEventListener(MouseEvent.CLICK, this.close);
			this.releasebtn.addEventListener(MouseEvent.CLICK, this.releaseClick);
			this.fightbtn.addEventListener(MouseEvent.CLICK, this.fightClick);
			this.restbtn.addEventListener(MouseEvent.CLICK, this.restClick);
			this.upBtn.addEventListener(MouseEvent.CLICK, this.revolution);
			this.czjnbtn.addEventListener(MouseEvent.CLICK, this.reSkills);
			this.czsxbtn.addEventListener(MouseEvent.CLICK, this.reAttribs);
			this.prePage.addEventListener(MouseEvent.CLICK, this.prePageClick);
			this.nextPage.addEventListener(MouseEvent.CLICK, this.nextPageClick);
			EventManager.getIns().registerListen(GameEventType.CLOSE_MANAGE_PET,__closePetInterface);
			this.setPetList();
		}
		
		private function removed(param1:Event):void
		{
			this.btn_close.removeEventListener(MouseEvent.CLICK, this.close);
			this.releasebtn.removeEventListener(MouseEvent.CLICK, this.releaseClick);
			this.fightbtn.removeEventListener(MouseEvent.CLICK, this.fightClick);
			this.restbtn.removeEventListener(MouseEvent.CLICK, this.restClick);
			this.upBtn.removeEventListener(MouseEvent.CLICK, this.revolution);
			this.czjnbtn.removeEventListener(MouseEvent.CLICK, this.reSkills);
			this.czsxbtn.removeEventListener(MouseEvent.CLICK, this.reAttribs);
			this.prePage.removeEventListener(MouseEvent.CLICK, this.prePageClick);
			this.nextPage.removeEventListener(MouseEvent.CLICK, this.nextPageClick);
			EventManager.getIns().removeListen(GameEventType.CLOSE_MANAGE_PET,__closePetInterface);
			this.removedPetList();
			EventManager.getIns().dispatchEvent(GameEventType.CONTINUE_GAME);
		}
		
		private function reSkills(param1:MouseEvent):void
		{
			if (!this.pif)
			{
				NotificationManager.alert("请先选择宠物");
				return;
			}
			if (this.player.getStuffNumByName("cwjnxld") > 0)
			{
				this.pif.refreshPetAllSkillByLevel();
				this.setPetAllSkill();
				this.player.removeEquipFormBag("cwjnxld", 2, 1);
				NotificationManager.alert("重置完成");
			}
			else
			{
				NotificationManager.alert("宠物技能洗练丹不足！");
			}
		}
		
		private function reAttribs(param1:MouseEvent):void
		{
			if (!this.pif)
			{
				NotificationManager.alert("请先选择宠物");
				return;
			}
			if (this.player.getStuffNumByName("cwzzxld") > 0)
			{
				this.pif.refreshThreeAttributeByImmortality();
				this.setShow();
				this.player.removeEquipFormBag("cwzzxld", 2, 1);
				NotificationManager.alert("重置完成");
			}
			else
			{
				NotificationManager.alert("宠物属性洗练丹不足！");
			}
		}
		
		private function prePageClick(param1:MouseEvent):void
		{
			this.setCurPage(-1);
			if (this.btnState)
			{
				if (this.lastBtn != "")
				{
					this[this.lastBtn].upState = this.btnState;
				}
			}
			this.lastBtn = "prePage";
			this.btnState = param1.target.upState;
			param1.target.upState = param1.target.downState;
		}
		
		private function nextPageClick(param1:MouseEvent):void
		{
			this.setCurPage(1);
			if (this.btnState)
			{
				if (this.lastBtn != "")
				{
					this[this.lastBtn].upState = this.btnState;
				}
			}
			this.lastBtn = "nextPage";
			this.btnState = param1.target.upState;
			param1.target.upState = param1.target.downState;
		}
		
		private function plClick(param1:MouseEvent):void
		{
			var _loc4_:* = null;
			var _loc2_:TextFormat = new TextFormat();
			_loc2_.color = "0x381d09";
			var _loc3_:uint = int(String(param1.currentTarget.name).substr(7, 1));
			if ((_loc4_ = this.findPetInCurrentPetListById(_loc3_ + (this.curPage - 1) * 5)) == null)
			{
				throw new Error("获取选择的宠物为null");
			}
			var _loc5_:uint = this.player.petList.length;
			while (_loc5_-- > 0)
			{
				if (this.getChildByName("petlist" + _loc5_))
				{
					this.getChildByName("petlist" + _loc5_)["petname"].setTextFormat(_loc2_);
				}
			}
			_loc2_.color = "0xfdfcba";
			param1.currentTarget.petname.setTextFormat(_loc2_);
			this.setPetInfomation(_loc4_);
			this.addPetHead();
		}
		
		private function releaseClick(param1:MouseEvent):void
		{
			var _loc1_:TipBox = new TipBox("您确定要放生这只宠物吗?", this.desertPet);
			_loc1_.add();
		}
		
		private function desertPet(param1:Event):void
		{
			if (!this.pif)
			{
				NotificationManager.alert("请先选择宠物");
				return;
			}
			var _loc2_:uint = this.player.petList.indexOf(this.pif);
			if (_loc2_ != -1)
			{
				this.player.petList.splice(_loc2_, 1);
			}
			if (this.pif.isFight == 1)
			{
				this.sendHeroRefreshPet();
			}
			this.pif = null;
			this.setShow();
			this.setPetList();
			if (this.pethead)
			{
				if (contains(this.pethead))
				{
					this.removeChild(this.pethead);
				}
				this.pethead.destroy();
				this.pethead = null;
			}
		}
		
		private function revolution(param1:MouseEvent):*
		{
			if (!this.pif)
			{
				NotificationManager.alert("请先选择宠物");
				return;
			}
			if (this.pif.level >= 50)
			{
				if (this.pif.petName == "monkey3")
				{
					this.pif.theFourShape();
					this.setShow();
					this.AfterSuperRevolution();
					NotificationManager.alert("恭喜您,宠物成功超进化！");
				}
				else if (this.pif.petName == "horse3")
				{
					this.pif.theFourShape();
					this.setShow();
					this.AfterSuperRevolution();
					NotificationManager.alert("恭喜您,宠物成功超进化！");
				}
				else if (this.pif.petName == "tigress3")
				{
					this.pif.theFourShape();
					this.setShow();
					this.AfterSuperRevolution();
					NotificationManager.alert("恭喜您,宠物成功超进化！");
				}
				else if (this.pif.petName == "turtle3")
				{
					this.pif.theFourShape();
					this.setShow();
					this.AfterSuperRevolution();
					NotificationManager.alert("恭喜您,宠物成功超进化！");
				}
				else if (this.pif.petName == "phoenix3")
				{
					this.pif.theFourShape();
					this.setShow();
					this.AfterSuperRevolution();
					NotificationManager.alert("恭喜您,宠物成功超进化！");
					
				}
				else if (this.pif.petName == "dragon3")
				{
					this.pif.theFourShape();
					this.setShow();
					this.AfterSuperRevolution();
					NotificationManager.alert("恭喜您,宠物成功超进化！");
					
				}
				else if (this.pif.petName == "rabbit3")
				{
					this.pif.theFourShape();
					this.setShow();
					this.AfterSuperRevolution();
					NotificationManager.alert("恭喜您,宠物成功超进化！");
					
				}
				else if (this.pif.petName == "roomhorse3")
				{
					this.pif.theFourShape();
					this.setShow();
					this.AfterSuperRevolution();
					NotificationManager.alert("恭喜您,宠物成功进化！");
					
				}
				else if (this.pif.petName == "mouse3")
				{
					this.pif.theFourShape();
					this.setShow();
					this.AfterSuperRevolution();
					NotificationManager.alert("恭喜您,宠物成功进化！");
					
				}
				else
				{
					NotificationManager.alert("该宠物暂时未开放超进化!");
					
				}
			}
			else
			{
				NotificationManager.alert("宠物等级未达到50!");
				
			}
		}
		
		private function fightClick(param1:MouseEvent):void
		{
			if (!this.pif)
			{
				NotificationManager.alert("请选择宠物出战");
				return;
			}
			var _loc2_:PetDataVO = this.player.findCurrentPet();
			if (this.pif.hp <= 0)
			{
				NotificationManager.alert("您的宠物需要休息了");
				return;
			}
			if (_loc2_)
			{
				_loc2_.isFight = 0;
			}
			if (this.pif)
			{
				this.pif.isFight = 1;
			}
			this.refreshPetList();
			this.sendHeroRefreshPet();
		}
		
		private function restClick(param1:MouseEvent):void
		{
			if (this.pif)
			{
				this.pif.isFight = 0;
				this.refreshPetList();
			}
			this.sendHeroRefreshPet();
		}
		
		private function __closePetInterface(param1:GameEvent):void
		{
			this.close(null);
		}
		
		private function close(param1:MouseEvent):void
		{
			if (this.pethead)
			{
				if (contains(this.pethead))
				{
					this.removeChild(this.pethead);
				}
				this.pethead.destroy();
				this.pethead = null;
			}
			if (this.parent)
			{
				this.parent.removeChild(this);
			}
		}
	}
}
